package com.activitys;

import android.app.Activity;
import android.os.Build;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.WindowManager;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import com.jaeger.library.StatusBarUtil;

public class ActionBarActivity extends AppCompatActivity {
    private int menuLayout;

    /**
     * 设置透明度
     *
     * @param alphaValue
     */
    protected void setTranslucent(int alphaValue) {
        StatusBarUtil.setTranslucent(this, alphaValue);
    }

    /**
     * 设置颜色
     *
     * @param color
     */
    protected void setActionBarColor(Activity activity, int color) {
        StatusBarUtil.setColor(activity, color);
    }

    /**
     * 隐藏状态栏
     */
    protected void hideStatusBar(boolean status) {
        if (status) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_SHORT_EDGES;
                getWindow().setAttributes(lp);
            } else {
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            }
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                WindowManager.LayoutParams lp = getWindow().getAttributes();
                lp.layoutInDisplayCutoutMode = WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_DEFAULT;
                getWindow().setAttributes(lp);
            } else {
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN, WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            }
        }
    }

    /**
     * 隐藏标题栏
     */
    protected void hideTitleBar(boolean status) {
        ActionBar mActionBar = getSupportActionBar();
        if (mActionBar != null) {
            if (status) {
                mActionBar.hide();
            } else {
                mActionBar.show();
            }
        }
    }

    /**
     * 系统标题栏存在时
     * 是否显示返回按钮
     *
     * @param show
     */
    public void showSystemBack(boolean show) {
        ActionBar mActionBar = getSupportActionBar();
        if (show) {
            mActionBar.setDisplayShowTitleEnabled(true);
            mActionBar.setDisplayHomeAsUpEnabled(true);
        }
        mActionBar.setDisplayShowHomeEnabled(show);
    }

    /**
     * 设置标题栏文字
     */
    public void setBarTitle(String str) {
        ActionBar mActionBar = getSupportActionBar();
        mActionBar.setTitle(str);
    }

    /**
     * 标题栏右侧事件
     */
    public void rightItemMenu(int layout) {
        this.menuLayout = layout;
    }


    /**
     * 系统标题栏存在时
     * 标题栏事件处理
     *
     * @param item
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                this.finish();
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        if (menuLayout != 0) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(menuLayout, menu);
        }
        return true;
    }
}
